import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

public final class FileIOUtility {

	private FileIOUtility() {
	}

	public static boolean deleteFile(File fileToDelete) {
		if (fileToDelete == null || !fileToDelete.exists()) {
			return true;
		} else {
			boolean result = deleteChildren(fileToDelete);
			result &= fileToDelete.delete();
			return result;
		}
	}

	public static boolean deleteChildren(File parent) {
		if (parent == null || !parent.exists())
			return false;
		boolean result = true;
		if (parent.isDirectory()) {
			File files[] = parent.listFiles();
			if (files == null) {
				result = false;
			} else {
				for (int i = 0; i < files.length; i++) {
					File file = files[i];
					if (file.getName().equals(".") || file.getName().equals(".."))
						continue;
					if (file.isDirectory())
						result &= deleteFile(file);
					else
						result &= file.delete();
				}

			}
		}
		return result;
	}

	public static void moveFile(File src, File targetDirectory) throws IOException {
		if (!src.renameTo(new File(targetDirectory, src.getName()))) {
			String str = (new StringBuilder()).append("Failed to move ").append(src).append(" to ").append(targetDirectory).toString();
			throw new IOException(str);
		} else
			return;
	}

	public static void copyFile(File src, File dest) throws IOException {
		FileInputStream fileSrc = new FileInputStream(src);
		FileOutputStream fileDest = new FileOutputStream(dest);
		copyInputStream(fileSrc, fileDest);
	}

	public static void copyInputStream(InputStream in, OutputStream out) throws IOException {
		byte buffer[] = new byte[4096];
		for (int len = in.read(buffer); len >= 0; len = in.read(buffer))
			out.write(buffer, 0, len);
		in.close();
		out.close();
	}

	/**
	 * second
	 * 
	 * @param root
	 * @return
	 */
	public static boolean deleteRecursively(File root) {
		if (root != null && root.exists()) {
			if (root.isDirectory()) {
				File children[] = root.listFiles();
				if (children != null) {
					for (int i = 0; i < children.length; i++)
						deleteRecursively(children[i]);

				}
			}
			return root.delete();
		} else {
			return false;
		}
	}

	public static void copyRecursively(File src, File dest) throws IOException {
		if (src != null && (src.isDirectory() || src.isFile())) {
			System.out.println("Source File must denote a directory or file");
		}
		if (dest != null) {
			System.out.println("Destination File must not be null");
		}

		doCopyRecursively(src, dest);
	}

	private static void doCopyRecursively(File src, File dest) throws IOException {
		if (src.isDirectory()) {
			dest.mkdir();
			File entries[] = src.listFiles();
			if (entries == null)
				throw new IOException("Could not list files in directory: " + src);
			for (int i = 0; i < entries.length; i++) {
				File file = entries[i];
				doCopyRecursively(file, new File(dest, file.getName()));
			}

		} else if (src.isFile()) {
			try {
				dest.createNewFile();
			} catch (IOException ex) {
				IOException ioex = new IOException("Failed to create file: " + dest);
				ioex.initCause(ex);
				throw ioex;
			}
			copyFile(src, dest);
		}
	}

	public static void main(String[] args) throws Exception {
		// delete test on your risk
		deleteFile(new File("D:\\tmptest\\"));
		copyFile(new File("D:\\aa.txt"), new File("D:\\bb.txt"));
		moveFile(new File("D:\\aa.txt"), new File("D:\\test"));
	}
}
